package com.quanyan.runningriding.web.running;

import com.quanyan.api.APIResponse;
import com.quanyan.common.utils.StringUtil;
import com.quanyan.runningriding.entity.vo.run.resp.RunUserInfo;
import com.quanyan.runningriding.service.RunEditProfileService;
import com.quanyan.runningriding.util.RunEnum;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * Created by liubin on 2016/12/19.
 */

@RestController
@RequestMapping(value = "/api/run")
public class RunEditProfileController {
    @Autowired
    private RunEditProfileService runEditProfileService;
    private Logger logger = LoggerFactory.getLogger(RunEditProfileController.class);

    @RequestMapping(value = "/editProfile",method = RequestMethod.POST)
    public APIResponse<?> editProfile(@RequestBody Map<String, Object> paramMap) {
        String userId = MapUtils.getString(paramMap, "userId");
        String photo = MapUtils.getString(paramMap,"photo");
        String nickname = MapUtils.getString(paramMap,"nickname");
        Byte gender = MapUtils.getByte(paramMap,"gender");
        Double height = MapUtils.getDouble(paramMap,"height");
        Double weight = MapUtils.getDouble(paramMap,"weight");
        Integer age = MapUtils.getInteger(paramMap,"age");
        APIResponse<?> result = runEditProfileService.editProfile(userId,photo,nickname,gender,height,weight,age);
        if (result.isRet()) {
            return APIResponse.returnSuccess(result.getData());
        } else {
            return APIResponse.returnFail(result.getErrmsg());
        }
    }

    @RequestMapping(value = "/displayUserInfo",method =  RequestMethod.POST)
    public APIResponse<RunUserInfo> displayUserInfo(@RequestBody Map<String,Object> paramMap){
        String userId=MapUtils.getString(paramMap, "userId");
        if(StringUtil.isBlankOrNull(userId)) {
            APIResponse.returnFail(RunEnum.PARAMETER_NOT_VALID);
        }
        return runEditProfileService.displayUserInfo(userId);
    }
}
